//
//  YZXPhotoCell.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/31.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

protocol YZXPhotoCellDelegate: NSObjectProtocol{
    // 加号按钮的点击
    func photoCellDidClickAddButton(cell:YZXPhotoCell)
    func photoCellDidClickDeleteButton(cell:YZXPhotoCell)
}

class YZXPhotoCell: UICollectionViewCell {
    // 代理属性
    weak var delegate: YZXPhotoCellDelegate?
    
    //MARK:-
    func setupAddBtn() {
        self.addButton.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        // 隐藏删除按钮
        self.deleteButton.hidden = true
    }
    
    var image: UIImage? {
        didSet {
            addButton.setImage(image, forState: UIControlState.Normal)
            // 显示删除按钮
            deleteButton.hidden = false
        }
    }
    
    
    //MARK:-view的生命周期
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {

        self.contentView.addSubview(addButton)
        self.contentView.addSubview(deleteButton)
        
        deleteButton.translatesAutoresizingMaskIntoConstraints = false
        addButton.translatesAutoresizingMaskIntoConstraints = false
        // 约束
        self.contentView.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.contentView.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.contentView.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.contentView.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        // 删除按钮
        self.contentView.addConstraint(NSLayoutConstraint(item: deleteButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.contentView.addConstraint(NSLayoutConstraint(item: deleteButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    }
    //MARK:- 按钮点击事件
    @objc private func btnDidClick() {
        delegate?.photoCellDidClickAddButton(self)
    }
    @objc private func close() {
        delegate?.photoCellDidClickDeleteButton(self)
    }
    
    
    //MARK:- 懒加载
    // 添加按钮
    private lazy var addButton:UIButton = {
       let btn = UIButton()
        btn.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "btnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 删除按钮
    private lazy var deleteButton:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
